﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComplexNumbers
{
    public class Polar : Complex
    {
        private double abs;
        private double angle;

        public Polar(double abs, double angle)
        {
            this.abs = abs;
            this.angle = angle;
        }

        override public double Real
        {
            get { return abs; }
        }

        override public double Imaginary
        {
            get { return angle; }
        }

        public static implicit operator Descard(Polar num)
        {
            double real = num.Real * Math.Cos(num.Imaginary);
            double imaginary = num.Real * Math.Sin(num.Imaginary);
            return new Descard(real, imaginary);
        }

        public static Polar operator +(Polar first, Polar second)
        {
            return (Polar)((Descard)first + (Descard)(second));
        }

        public static Polar operator -(Polar first, Polar second)
        {
            return (Polar)((Descard)first - (Descard)(second));
        }

        public static Polar operator *(Polar first, Polar second)
        {
            double angle = first.Imaginary + second.Imaginary;
            while (angle > 2 * Math.PI)
            {
                angle -= 2 * Math.PI;
            }
            return new Polar(first.Real * second.Real, angle);
        }

        public static Polar operator /(Polar first, Polar second)
        {
            if (second.Real == 0)
            {
                throw new DivideByZeroException();
            }

            return new Polar(first.Real / second.Real, first.Imaginary - second.Imaginary);
        }

        public new virtual string ToString()
        {
            return String.Format("{0}(cos({1}) + isin({1}))",Real,Imaginary);
        }
    }
}
